package com.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.dto.UserDTO;
import com.test.entity.User;
import com.test.mapper.UserMapper;
import com.test.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * serviceimpl
 *
 * @author kuke
 * @since 2022-03-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<User> queryUserList(Long id) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //wrapper.eq("id", id);

        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().eq(User::getId, id);

        LambdaQueryWrapper<User> wrapper2 = new LambdaQueryWrapper<User>();
        wrapper2.eq(User::getId, id);
        User user = new User();


        List<User> bannerItems = new LambdaQueryChainWrapper<User>(this.baseMapper)
                .eq(User::getId, id)
                .list();
        //one可不能随便用的。.one()相当于selectOne如果数据库有多条记录，返回多条会出错的吧


        IPage<User> mapPage = new Page<>(2, 2, true);
        IPage<User> mapIPage = this.baseMapper.listAll(mapPage, wrapper);
        System.out.println("总页数： " + mapIPage.getRecords());
        System.out.println("总记录数： " + mapIPage.getTotal());

        return null;
    }

    @Override
    public List<User> queryList(User mo) {
        return this.baseMapper.queryList(mo);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.baseMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.baseMapper.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public IPage<User> getPageList(UserDTO mo) {
        IPage<User> pageVO = new Page<>();
        List<User> userList = new ArrayList<User>();
        Integer count = this.baseMapper.queryPageListCount(mo);
        if (0 != count) {
            long offset = mo.offset();
            userList = this.baseMapper.queryPageList(offset, mo.getPageSize(), mo);
            pageVO.setTotal(count);
            pageVO.setRecords(userList);

        }
        return pageVO;
    }

}
